const mongoose = require("mongoose");
const moment = require("moment");
class Db {
	constructor(model) {
		this.model = model;
	}
	_connect(){
		// 判断是否已经连接成功了
		if(mongoose.connection.readyState === 1) return Promise.resolve();
		return mongoose.connect("mongodb://127.0.0.1:27017/weibo",{
			serverSelectionTimeoutMs:10000
		})
	}
	// 插入一条记录
	// insertObj:插入的文档记录
	async insertOne(insertObj){
		await this._connect();
		return this.model(insertObj).save();
	}
}
// 结构约束规则
const schema = new mongoose.Schema({
	// 微博内容
	context: {
		type: String,
		required: true,
		unique: true
	},
	// 顶的数量
	topNum: {
		type: Number,
		default: 0
	},
	// 踩的数量
	downNum: {
		type: Number,
		default: 0
	},
	// 增加的时间
	addTime: {
		type: String
	}
}, {
	versionKey: false
})
// model
const model = mongoose.model("contextList", schema, "contextList");
const obj = new Db(model);
obj.insertOne({
	context:"abc",
	addTime:moment().format("YYYY-MM-DD HH:mm:ss")
}).then(value=>{
	console.log("添加成功",value)
})
